﻿

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Reflection;
using System.Web;

namespace Viewer.BusinessObject.BusinessEntities
{
    [Serializable()]
    public partial class NSKiLuat : BusinessBase
    {
        NSQuyetDinh quyetDinh = new NSQuyetDinh();
        CommonCode common = new CommonCode();
        DataClassesDataContext db = new DataClassesDataContext();
        public string TenCapKiLuat { set; get; }
        public string MaSo { get; set; }

        public void EditKL(int hdId, string tenKiLuat, int capKiLuatId, string hinhThuc, string ghiChu, int qdID,DateTime ngayHieuLuc)
        {
            List<NSKiLuat> lst = (List<NSKiLuat>)HttpContext.Current.Session["KL"];
            NSKiLuat pc = lst.Where(a => a.KiLuatId.Equals(hdId)).SingleOrDefault();
            pc.TenKiLuat = tenKiLuat;
            pc.GhiChu = ghiChu;
            pc.CapKiLuatId = capKiLuatId;
            pc.HinhThucKiLuat = hinhThuc;
            pc.TenCapKiLuat = common.LoadCommonCodeById(capKiLuatId);
            //chỉ thêm mới khi quyết định > 0.
            //Đã tồn tại qdID khi =0
            if (!ngayHieuLuc.Year.Equals(1))
            {
                pc.NgayHieuLuc = ngayHieuLuc;
            }
            if (qdID > 0)
            {
                pc.QuyetDinhID = qdID;
                pc.MaSo = quyetDinh.GetMaSo(qdID);
            }
            else
            {
                pc.MaSo = quyetDinh.GetMaSo(pc.QuyetDinhID.Value);
            }
        }
        public void DeleteKL(int hdId)
        {
            List<NSKiLuat> lst = (List<NSKiLuat>)HttpContext.Current.Session["KL"];
            NSKiLuat pc = lst.Where(a => a.KiLuatId.Equals(hdId)).SingleOrDefault();
            lst.Remove(pc);
        }
        public void DeleletAllKLByNhanVien(int nhanVienId)
        {
            var query = (from a in db.NSKiLuats
                         where a.NhanVienId.Equals(nhanVienId)
                         select a).ToList();
            if (query.Count > 0)
            {
                db.NSKiLuats.DeleteAllOnSubmit(query);
                db.SubmitChanges();
            }

        }
        public void UpdateKL_NhanVien(int nhanVienId, List<NSKiLuat> kiLuatObject)
        {

            var queryNBLByNhanVienId = (from a in db.NSKiLuats
                                        where a.NhanVienId.Equals(nhanVienId)
                                        select a).ToList();
            if (queryNBLByNhanVienId.Count > 0)
            {
                db.NSKiLuats.DeleteAllOnSubmit(queryNBLByNhanVienId);
                db.SubmitChanges();
            }

            foreach (NSKiLuat pc in kiLuatObject)
            {
                NSKiLuat item = new NSKiLuat();
                CopyNSKiLuat(pc, item);
                db.NSKiLuats.InsertOnSubmit(item);
            }
            db.SubmitChanges();

        }       
        public List<NSKiLuat> LoadKLByNhanVienId(int nhanVienId)
        {

            var query = (from a in db.NSKiLuats
                         where a.NhanVienId.Equals(nhanVienId)
                         orderby a.TenKiLuat descending
                         select a).ToList();
            foreach (NSKiLuat item in query)
            {
                item.TenCapKiLuat = common.LoadCommonCodeById((int)item.CapKiLuatId);
                item.MaSo = item.QuyetDinhID.HasValue ? quyetDinh.GetMaSo(item.QuyetDinhID.Value) : string.Empty;
            }

            return query;

        }
        public NSKiLuat GetKiLuat(int nhanVienId, string tenKiLuat, string hinhThucKiLuat, int capKiLuatId, string ghiChu, int qdID, DateTime ngayHieuLuc)
        {
            var db = new DataClassesDataContext();
            NSKiLuat hd = new NSKiLuat();
            hd.KiLuatId = 0 - DateTime.Now.Second;
            hd.TenKiLuat = tenKiLuat;
            hd.GhiChu = ghiChu;
            hd.NhanVienId = nhanVienId;
            hd.HinhThucKiLuat = hinhThucKiLuat;
            hd.CapKiLuatId = capKiLuatId;
            hd.TenCapKiLuat = common.LoadCommonCodeById(capKiLuatId);
            hd.QuyetDinhID = qdID;
            hd.MaSo = quyetDinh.GetMaSo(qdID);
            if (!ngayHieuLuc.Year.Equals(1))
            {
                hd.NgayHieuLuc = ngayHieuLuc;
            }
            return hd;
        }
        private void CopyNSKiLuat(NSKiLuat src, NSKiLuat des)
        {

            des.TenKiLuat = src.TenKiLuat;
            des.GhiChu = src.GhiChu;
            des.NhanVienId = src.NhanVienId;
            des.HinhThucKiLuat = src.HinhThucKiLuat;
            des.CapKiLuatId = src.CapKiLuatId;
            des.TenCapKiLuat = src.TenCapKiLuat;
            des.QuyetDinhID = src.QuyetDinhID;
            if (src.NgayHieuLuc.HasValue)
            {
                des.NgayHieuLuc = src.NgayHieuLuc;
            }
        }
    }
}